/*
 * @Author: 哈尔滨大拐 3181845089@qq.com
 * @Date: 2022-08-04 16:39:04
 * @LastEditors: 哈尔滨大拐 3181845089@qq.com
 * @LastEditTime: 2022-08-08 14:39:33
 * @FilePath: \mygit\个人网站\admin\js\blog-list.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
(function () {
    let totalPage = 0
    let pageNum = getQueryString('pageNum') || 1

    fetchData()
    function fetchData() {
        axios.get('/admin/getBlogs', {
            params: {
                pageNum: pageNum,
                pageSize: 3
            }
        }).then(res => {
            console.log(res);
            totalPage = res.data.totalPage
            ininitialPagination()

            // 博客
            const blogs = res.data.data
            // console.log(blogs);
            const tBody = document.querySelector('.blog-list tbody')
            blogs.forEach(blog => {
                const tr = document.createElement('tr')
                tr.innerHTML = `
                <th>${blog.id}</th>
                <td><img src="${imgUrl(blog.image)}" alt=""></td>
                <td class="title">${blog.title}</td>
                <td>${blog.abstract}</td>
                <td>${blog.createTime}</td>
                <td>  
                  <button class="btn btn-primary edit">编辑</button>
                  <button class="btn btn-danger delete" onclick="deleteHandler(${blog.id})">删除</button>
                </td>
                `
                tBody.appendChild(tr)
            });
        }).catch(err => {
            let res = err.response;
            if(res.status == 401){
              alert('您未授权本操作，请重新登录');
              location.href = 'login.html';
            }
          })
    }


    // 删除博客
    function deleteHandler(id) {
        axios.get('/admin/deleteBlog', {
            params: { id: id }
        }).then(res => {
            console.log(res);
            if (res.data == 'success') {
                // 提示动画（1s）
                setTimeout(() => {
                    location.reload()
                }, 1000);
            }
        })
    }


// 翻页
    function ininitialPagination() {
        const ul = document.querySelector('.pagination')

        for (let i = 1; i <= totalPage; i++) {
            const li = document.createElement('li')
            li.className = `paginate_button page-item ${pageNum == i ? 'active' : ''}`
            li.innerHTML = `<a href="blog-list.html?pageNum=${i}" aria-controls="dataTableExample" data-dt-idx="1"
            tabindex="0" class="page-link">${i}</a>`
            ul.appendChild(li)
        }

    }


// 退出
const btn = document.querySelector('#btn')
let myToken = localStorage.getItem("token");
console.log(myToken);
btn.addEventListener('click' , function(){
    console.log(111);
    localStorage.removeItem('token');
    location.reload()
   
})



    
})()